<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #box{
            width: 800px;
            margin: 200px auto;
            text-align: center;
        }
    </style>
</head>
<body>
    <div id="box">
        <img src="../img/icon_01.png" alt="" id = "icon">
        <div>
            <button id = "prev">上一张</button>
            <button id = "next">下一张</button>
        </div>
    </div>
    <script>
        window.onload = function(){
            //1、获取需要的标签
            let icon = document.getElementById('icon');
            let prev = document.getElementById('prev');
            let next = document.getElementById('next');

            let currentIndex =1,minIndex=1,maxIndex = 9;

            // 2、监听按钮点击
            //2.1上一张
            prev.onclick = function(){//事件监听
                if(currentIndex === minIndex){
                    currentIndex = maxIndex;
                }
                else{
                    currentIndex--;
                }
                // icon.src = `../img/icon_0${currentIndex}.png`;
                icon.setAttribute('src',`../img/icon_0${currentIndex}.png`)
            }
            //2.2下一张
            next.onclick = function(){//事件监听
                if(currentIndex === maxIndex){//到达最大值边界
                    currentIndex = minIndex;
                }
                else{
                    currentIndex++;
                }
                // icon.src = `../img/icon_0${currentIndex}.png`
                icon.setAttribute('src',`../img/icon_0${currentIndex}.png`)
            }
        }
    </script>
</body>
</html>